﻿using ConsoleApp7;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Realize;
using System.Threading.Tasks;

namespace WebApplication9.Controllers.ClientSet
{
    public class CPCController : Controller
    {
        private readonly ILogger<CPCController> _logger;
        private readonly IConfiguration configuration;

        public CPCController(ILogger<CPCController> logger, IConfiguration configuration)
        {
            this._logger = logger;
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            ViewBag.s = "TextController2.Index";
            return View();
        }
        public IActionResult Add()
        {
            ViewBag.s = "TextController2.Add";
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Add(string attribute_kind, string attribute_name)
        {
            ICpc cpc = new Cpc();
            Config_Public_Char cpc2 = new Config_Public_Char() {attribute_kind = attribute_kind,attribute_name=attribute_name };
            int i = await cpc.Insert(cpc2, configuration["SqlServer"]);
            if (i > 0)
            {
                return RedirectToAction("Index");
            }
            else 
            {
                Log_file_storage.LogDebug(_logger, "第42行");
            }
            return View();
        }

        public async Task<IActionResult> Index2(int i,int p)
        {
            ICpc cpc = new Cpc();
            List<Config_Public_Char> zhi = await cpc.Server(configuration["SqlServer"]);
            List<Config_Public_Char> zhi2 = zhi.Skip((i - 1) * p).Take(p).ToList();
            PagedViewModel<Config_Public_Char> cc = new PagedViewModel<Config_Public_Char>() {itemas = zhi2,total = zhi.Count }; 
            return Ok(new
            {
                list = cc
            });
        }
        public async Task<IActionResult> Delete(int id) 
        {
            ICpc cpc = new Cpc();
            int i = await cpc.Delete(id,configuration["SqlServer"]);
            if (i > 0) 
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            return Ok(new
            {
                zhi = "onok"
            }); ;
        }
        public async Task<IActionResult> Update(int id)
        {
            HttpContext.Session.SetInt32("CID", id);
            ICpc cpc = new Cpc();
            List<Config_Public_Char> zhi = await cpc.Server2(id, configuration["SqlServer"]);
            ViewBag.sd = zhi[0];
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Update(string attribute_kind,string attribute_name)
        {
            int id = (int)HttpContext.Session.GetInt32("CID");
            ICpc cpc = new Cpc();
            int i = await cpc.Update(id, attribute_kind, attribute_name, configuration["SqlServer"]);
            if (i > 0)
            {
                return RedirectToAction("Index", "CPC");
            }
            return Content("失败");
        }
    }
}
